+function ($) {

    var LdSelect = function (f, e) {
        this.$element = $(f);
        this.options = $.extend({}, this.$element.data(), e);
        this.init()
    };

    LdSelect.prototype.init = function () {
        var ref = this.options.ref, refUrl = this.options.refUrl;
        if (ref && refUrl) {

            if (this.$element.datetimepicker) {

                this.$element.bind("dp.change", {
                    ref: ref,
                    refUrl: refUrl,
                    targer: this.$element.find("input")
                }, _onchange);
            } else {
                this.$element.unbind("change", _onchange).bind("change", {
                    ref: ref,
                    refUrl: refUrl,
                    targer: this.$element
                }, _onchange);
            }
        }
        this.$element.trigger("change");
    }

    var _onchange = function(event){
        var arrayRef = event.data.ref.split(",");
        $.each(arrayRef, function (index) {
            var $ref = $("#" + arrayRef[index]), dataUrl = event.data.refUrl, value = event.data.targer.val();
            if ($ref.length == 0) return false;
            if (value == null) return false;
            const reg = new RegExp('\\{(.+?)\}');
            if (reg.test(dataUrl)) {
                var macthValue = reg.exec(dataUrl);
                dataUrl = dataUrl.replace(macthValue[0], value);
            }
            Bolt.ajax('doajax', {
                type: 'GET',
                data: {},
                cache: false,
                beforeSend: function (request) {
                    let token = window.localStorage.getItem('report-Access-Token');
                    if (token != null && token != "") {
                        request.setRequestHeader(Bolt.tokenName, token);
                    }
                },
                url: dataUrl,
                okCallback: function (content) {
                    _comboxRefresh($ref, content);
                }
            })
        })

    };

    var _comboxRefresh = function($select, json){
        if (!json) return;
        var html = '', selected = '';
        var nextVal = $select.data('value');
        $.each(json, function (k, v) {

            if (typeof nextVal !== 'undefined') selected = k == nextVal ? ' selected' : ''
            html += '<option value="' + k + '"' + selected + '>' + v + '</option>';

        });

        $select.empty();
        $select.html(html);
        $select.trigger("change");
        $select.selectpicker('refresh');
    };

    var _comboxReset = function($select){
        var $box = $select.parents('div.select:first'),
            defaultValue = $box.find('>a').attr('default-value');
        $('#op_'+$box.attr('id')).find('>li a[value="'+defaultValue+'"]').trigger('click');
    };

    var Plugin = function(){
        return this.each(function () {
            var h = $(this);
            var g = h.data("ldSelect");
            var f = typeof e == "object" && e;
            if (!g) {
                h.data("ldSelect", (g = new LdSelect(this, f)))
            }
            if (typeof e == "string") {
                g[e]()
            }
        })
    }

    $.fn.ldSelect = Plugin
    $.fn.ldSelect.Constructor = LdSelect;
    Bolt.addComponent({
        name: "ldSelect",
        plugin: Plugin,
        runPoint: Bolt.eventType.afterAjaxPageShow,
        expr: "[data-event='ldSelect']",
        order: 800
    })

}(jQuery);